package com.jkl.service.dept;

import com.jkl.dao.dept.DeptMapper;
import com.jkl.entity.dept.Dept;
import com.jkl.service.dept.inter.DeptServiceInterface;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liuzhennan
 * @create 2020-11-06 19:02
 */
@Service
@Slf4j
public class DeptService implements DeptServiceInterface {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> getDeptList() {
        List<Dept> deptList = deptMapper.getDeptList();
        return deptList;
    }

    @Override
    public List<Dept> listWithTree(String deptId) {
        //1、查出所有分类
        List<Dept> deptList = deptMapper.getDeptList();
        //2、根据id组装分类
        //2.1、找到当前条件下的一级分类
        List<Dept> level1 = deptList.stream().filter(Dept ->
            Dept.getDeptCode().equals(deptId)
        ).map((i) ->{
            i.setChildren(getChidrens(i,deptList));
            return i;
        }).sorted((i1,i2)->{
            return (i1.getSequence()==null ? 0 : i1.getSequence()) - (i2.getSequence()==null ? 0 : i2.getSequence());
        }).collect(Collectors.toList());
        return level1;
    }

    @Override
    public List<Dept> citiesCounties(String deptId) {
        //1、查出所有分类
        List<Dept> deptList = deptMapper.getDeptLevelLt4();
        //2、找到当前条件下的一级分类
        List<Dept> collect = deptList.stream().filter(Dept ->
                Dept.getRowId().toString().equals(deptId)
        ).map((i) ->{
            i.setChildren(getChidrens(i,deptList));
            return i;
        }).sorted((i1,i2)->{
            return (i1.getSequence()==null ? 0 : i1.getSequence()) - (i2.getSequence()==null ? 0 : i2.getSequence());
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<Dept> rootDeptList(String deptId) {

        //查出所有比自己小的不包括自己的dept
        List<Dept> deptList = deptMapper.getChildrenDeptListById(deptId);
        Dept preDept = new Dept();
        int root = 2;
        Dept currList = deptMapper.getDept(deptId);
        int curr = currList.getLevel();
        int parentId = currList.getParentId();
        //将当前所在的dept放入list
        deptList.add(currList);
        //遍历直属的父亲然后放入进list
        for(int i = 0 ; i < (curr - 2) ; i++){
            preDept = deptMapper.getDeptByParenId(parentId);
//            if (preDept == null){
//                break;
//            }
            deptList.add(preDept);
            parentId = preDept.getParentId();
        }
        Dept temPreDept = preDept;
        if (preDept.getRowId() == null){
            temPreDept = deptMapper.getDept(deptId);
        }
        Dept finalPreDept = temPreDept;

        //将所有组合起来
        List<Dept> collect = deptList.stream().filter(Dept ->
                    Dept.getRowId().equals(finalPreDept.getRowId())
        ).map(i -> {
            i.setChildren(getChidrens(i,deptList));
            return i;
        }).sorted((i1,i2)->{
            return (i1.getSequence()==null ? 0 : i1.getSequence()) - (i2.getSequence()==null ? 0 : i2.getSequence());
        }).collect(Collectors.toList());

        return collect;
    }

    //递归查找所有部门的子部门
    private List<Dept> getChidrens(Dept root,List<Dept> all){

        List<Dept> children = all.stream().filter(Dept ->
            Dept.getParentId().equals(root.getRowId())
        ).map(dept -> {
            dept.setChildren(getChidrens(dept,all));
            return dept;
        }).sorted((i1,i2)->{
            return (i1.getSequence()==null ? 0 : i1.getSequence()) - (i2.getSequence()==null ? 0 : i2.getSequence());
        }).collect(Collectors.toList());

        return children;
    }


}
